//userService
const db = require("../../config/db.config"); //引入数据库封装模块

const userService = {
  searchUser: async () =>{
      //查询users表
    const sql = 'select * from users'
    const data = await db.query(sql, '')
    return data
  },
  addUser: async (params) =>{
    //新增users表
    const sql = `insert into users (name) values (?)`
    const data = await db.query(sql, params.name)
    return data
  },
  delUser: async (params) =>{
    //新增users表
    const sql = `delete from users where id = ?`
    const data = await db.query(sql, params.id)
    return data
  },
  updateUser: async (params) =>{
    //新增users表
    const sql = `update users set name = ? where id = ?`
    const data = await db.query(sql, [params.name, params.id])
    return data
  },
  loginChecked: async (params) =>{
    //登录验证
    const sql = 'select * from users where username = ? and password = ?'
    const data = await db.query(sql, [params.username, params.password])
    return data
  },
  updateUserInfo: async (params) =>{
    const sql = `update users set name = ?, sex = ? where id = ?`
    const data = await db.query(sql, [params.name, params.sex, params.id])
    return data
  },
  saveRefreshToken: async (params) =>{
    const sql = `insert into refreshtoken (UserId, refreshToken, expiresAt) values (?, ?, ?)`
    const data = await db.query(sql, [params.id, params.refreshToken, params.expiresAt])
    return data
  },
  findRefreshToken: async (params) =>{
    console.log(params)
    // const sql = 'select * from refreshtoken where refreshToken = ? and expiresAt > ?'
    // const data = await db.query(sql, [params.refreshToken, params.expiresAt])

    const sql = 'select * from refreshtoken where refreshToken = ?'
    const data = await db.query(sql, params.token)
    return data
  },

  delRefreshToken: async (params) =>{
    //新增users表
    const sql = `delete from refreshtoken where id = ?`
    const data = await db.query(sql, params.id)
    return data
  },
}

module.exports = userService
